package com.qianfeng.listviewdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jackiechan on 16/3/11 上午11:24.
 * 版本: 1
 * 公司:北京千锋互联科技有限公司
 */
public class MyFragment extends Fragment implements AdapterView.OnItemClickListener {
    private ListView listView;
    private MyAdapter myAdapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fram, container, false);
        listView = (ListView) view.findViewById(R.id.listview);
//        myAdapter = new MyAdapter(getActivity());
//        myAdapter.setNum(100);
//        listView.setAdapter(myAdapter);
//        listView.setOnItemClickListener(this);
        List<Bean> lists = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            Bean bean = new Bean("发的说法是电风扇");
             lists.add(bean);
        }

        MyAdaper2 myAdaper2 =new MyAdaper2(getActivity(),lists);
        listView.setAdapter(myAdaper2);
        listView.setOnItemClickListener(this);
        return view;
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), Main2Activity.class);
        intent.putExtra("po", position);
        startActivityForResult(intent, 100);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
            String string = data.getStringExtra("data");
            int po = data.getIntExtra("po", -1);
            if (po != -1) {
                TextView viewWithTag = (TextView) listView.findViewWithTag("tv" + po);
                if (viewWithTag != null) {
                    viewWithTag.setText(string);
                    //在此更新数据源 但是不要刷新界面
                }
            }

        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
